<template>
  <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="Dialog Titile">
    <el-form ref="字典添加" :model="formData" :rules="rules" size="medium" label-width="100px"
             label-position="left">
      <el-form-item label="字典项文本" prop="itemText">
        <el-input v-model="formData.itemText" placeholder="请输入字典文本" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label="字典项值" prop="itemValue">
        <el-input v-model="formData.itemValue" placeholder="请输入字典项值" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入描述"
                  :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sortOrder">
        <el-input v-model="formData.sortOrder" placeholder="请输入1~999,值越大排序越靠前" clearable
                  :style="{width: '100%'}"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-dialog>

</template>

<style>

</style>

<script>
import axios from "axios"

export default {
  inheritAttrs: false,
  components: {},
  props: ["init","closeAdd","id","admin"],
  data() {
    return {
      formData: {
        itemText: "",
        itemValue: undefined,
        description: undefined,
        sortOrder: "",
      },
      rules: {
        itemText: [{
          required: true,
          message: '请输入字典文本',
          trigger: 'blur'
        }],
        itemValue: [{
          required: true,
          message: '请输入字典项值',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }],
        sortOrder: [{
          required: true,
          message: '请输入1~999,值越大排序越靠前',
          trigger: 'blur'
        }],
      },
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['字典添加'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {
      console.log(this.formData)
      console.log(this.formData.itemText)
      console.log(this.formData.itemValue)
      this.$refs['字典添加'].validate(valid => {
        if (!valid) return
        axios.post("http://localhost:8102/dictItem", {
          dictId:this.id,
          itemText: this.formData.itemText,
          itemValue: this.formData.itemValue,
          description: this.formData.description,
          sortOrder: this.formData.sortOrder,
          createBy: this.admin.adminId,
          updateBy:this.admin.adminId
        }).then((data)=>{
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          });
        }).catch((data)=>{
          this.$message({
            showClose: true,
            message: '未知原因,请联系管理员',
            type: 'error'
          });
        });
        this.init();
        this.closeAdd();
        this.close()
      })
    },
  }
}

</script>
